package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.constant.WorkOrderConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.WorkTypeObject;
import com.tbit.main.service.TokenService;
import com.tbit.main.service.WorkTypeObjectService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 工单类型
 *
 * @author LMY
 * @create 2021-09-08 16:57
 */
@RestController
@RequestMapping("/workTypeController")
public class WorkTypeObjectController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private WorkTypeObjectService workTypeObjectService;

    /**
     * 根据区域查询
     *
     * @param token
     * @param accountId
     * @return
     */
    @RequestMapping("getByAccountId")
    public JsonResult getByAccountId(String token, Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        AccountUser accountUser = tokenService.getAccount(token);

        List<WorkTypeObject> workTypes = workTypeObjectService.getByAccountId(accountId);

        return JsonResult.succ(workTypes);
    }

    /**
     * 添加或者修改
     *
     * @param token
     * @param workTypeObject
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.WORKTYPE, methods = MethodEnum.WORKTYPE_EDIT)
    public JsonResult add(String token, WorkTypeObject workTypeObject) {
        Assert.notNull(workTypeObject.getAccountId(), "AccountUser.agentId.notNull");
        AccountUser accountUser = tokenService.getAccount(token);

        WorkTypeObject workTypeDB = workTypeObjectService.getWorkType(workTypeObject.getWorkType());
        if (workTypeDB == null) {
            workTypeObjectService.add(workTypeObject);
            LogUtil.info(accountUser.getAccountUserId() + "添加工种：" + workTypeObject);
        } else {
            if (workTypeObject.getWorkType().equals(WorkOrderConstant.workType_SW) || workTypeObject.getWorkType().equals(WorkOrderConstant.workType_QT)) {
                throw new BaseException("WorkType.is.notRemove");
            }
            ObjectUtil.assign(workTypeObject, workTypeDB, null);
            workTypeObjectService.update(workTypeObject);

            LogUtil.info(accountUser.getAccountUserId() + "修改工种：" + workTypeObject);
        }

        return JsonResult.succ();
    }

    /**
     * 删除
     *
     * @param token
     * @param workType
     * @return
     */
    @RequestMapping("/del")
    public JsonResult del(String token, Integer workType) {

        AccountUser accountUser = tokenService.getAccount(token);

        if (workType.equals(WorkOrderConstant.workType_SW) || workType.equals(WorkOrderConstant.workType_QT)) {
            throw new BaseException("WorkType.is.notRemove");
        }

        WorkTypeObject workTypeObject = workTypeObjectService.getWorkType(workType);
        LogUtil.info(accountUser.getAccountUserId() + "删除工种：" + workTypeObject.toString());
        workTypeObjectService.del(workType);

        return JsonResult.succ();
    }

}
